<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Product extends Api
{
    public function index_handel($list)
    {
        $list['data'] = array_map(function($v){
            $v['money'] = 0;
            $v['sales'] = 0;
            if(!empty($v['specification_sku'])){
                foreach ($v['specification_sku'] as $vv){
                    if($v['money'] < $vv['price']){
                        $v['money'] = $vv['price'];
                    }
                    $v['sales'] += ($vv['sales']*1);
                }
            }
            return $v;
        },$list['data']);
        return parent::index_handel($list); // TODO: Change the autogenerated stub
    }
    //列表接口
    public function index()
    {
        if (input('post.limit')) {
            $this->limit = input('post.limit');
        }
        $post = array_filter(input('post.'));
        if(isset($post['where']) && is_array($post['where'])){
            foreach ($post['where'] as $k=>$v){
                if($k == 'name'){
                    $this->where[] = [$k,'like','%'.$v.'%'];
                }elseif($k == 'user_index_id'){
                    if(!isset($this->userinfo) || empty($this->userinfo)){
                        $this->checkLogin();
                    }
                    $this->where[] = [$k,'=',$this->userinfo['id']];
                }else{
                    $this->where[] = [$k,'=',$v];
                }
            }
        }
        $this->where[] = ['delete_time','=',null];
        $order = $post['order'] ?? [];
        if (!empty($order)) {
            if (is_string($order)) {
                if (strstr($order, ',')) {
                    $order = explode(',', $order);
                    $arr = [];
                    foreach ($order as $k => $v) {
                        $arr[$v] = 'desc';
                    }
                    $order = $arr;
                }
            }
        } elseif(!empty($this->order)){
            $order = $this->order;
        } else {
            $order = 'id desc';
        }
        if ($this->select) {
            $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->select();
        } else {
            if(input('find')){
                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->find();
            }else{

                $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->order($order)->where($this->where)->paginate($this->limit);
            }
        }
        if (!empty($list)) {
            $list = $list->toArray();
            $list = $this->index_handel($list);
            foreach ($list['data'] as $kk=>$vv){
                if(!empty($vv['images'])&&is_string($vv['images'])){
                    $images = explode(',',$vv['images']);
                    foreach ($images as $kkk=>$vvv)
                    {
                        $images[$kkk] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $vvv;
                    }    
                    $list['data'][$kk]['images'] = $images;
                }else{
                    $list['data'][$kk]['images'] = [];
                }
            }
            urladd($list);
        }
        return datasuccess($list);
    }
    //详情接口
    public function view()
    {
        if (!input('post.id')) {
            return msgerror('缺少参数id');
        }
        $this->where[] = ['id', '=', input('post.id')];
        $list = $this->model->with($this->with[request()->controller()]['function'] ?? [])->where($this->where)->find()->toArray();

        urladd($list);
        $time = strtotime($list['update_time']);
        $list['update_time'] = date('Y-m-d',$time);
        if(isset($this->with[request()->controller()])){
            foreach ($this->with[request()->controller()]['function'] as $v) {
                urladd($list[$v]);
            }
        }
        $images = explode(',',$list['images']);
        foreach ($images as $kkk=>$vvv)
        {
            $images[$kkk] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $vvv;
        }
        $list['images'] = $images;
        $list['money'] = 0;
        $list['sales'] = 0;
        $list['stock'] = 0;
        if(!empty($list['specification_sku'])){
            foreach ($list['specification_sku'] as $vv){
                if($list['money'] < $vv['price']){
                    $list['money'] = $vv['price'];
                }
                $list['sales'] += ($vv['sales']*1);
                $list['stock'] += ($vv['stock']*1);
            }
            unset($list['specification_sku']);
        }
        $list = $this->view_handel($list);
        return datasuccess($list);
    }
}
